﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace goMatic
{
    public partial class MOTION : System.Web.UI.UserControl
    {
        public event EventHandler CheckedChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
            load_mouse_events();
        }
        public MOTION()
        {
        }

        v_HMDeviceDetails vDetail;
        public v_HMDeviceDetails _Details
        {
            get { return vDetail; ; }
            set
            {
                if (value != null)
                {
                    vDetail = value;
                    dName.Text = vDetail.Name;
                    lDevice.Text = vDetail.Address;
                    if (vDetail.value != null)
                    {
                        _Motion =  Convert.ToBoolean(int.Parse(vDetail.value));
                        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
                        TimeSpan offset = tzi.GetUtcOffset(DateTime.Now);
                        lTime.Text = (value.C__createdAt.Value.LocalDateTime + offset).ToString("dd-MM HH:mm:ss");
                    }
                    else
                    {
                        Image2.ImageUrl = "~/Images/unknown.png";
                    }
                }
                else
                {
                    Image2.ImageUrl = "~/Images/unknown.png";
                }
            }
        }


        internal int iBright;
        public int _Brightness 
        {
            get { return iBright; }
            set 
            {
                iBright = value;
                lBright.Text = (value / 2.55).ToString("N0") + "%";
            } 
        }


        internal Boolean bMotion;
        public Boolean _Motion
        {
            get
            {
                return bMotion;
                //return cbState.Checked;
            }
            set
            {
                bMotion = value;
                if (bMotion)
                {
                    if (DateTime.Now.Subtract(vDetail.C__createdAt.Value.DateTime) > new TimeSpan(0, 2, 0))
                    {
                        Image2.ImageUrl = "~/Images/message_presence_32.png";
                    }
                    else
                    {
                        Image2.ImageUrl = "~/Images/message_presence_active_32.png";
                    }
                }
                else
                    Image2.ImageUrl = "~/Images/unknown.png";
            }
        }

        private void load_mouse_events()  
        {
        }

        protected void Panel2_Load(object sender, EventArgs e)
        {
            e.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            dbEntities oDB = new dbEntities();

            DateTime dStartTime = DateTime.Now.Subtract(new TimeSpan(0, 12, 0, 0));
            Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "dd. HH:mm";
            List<ChannelValues> oTVals = oDB.ChannelValues.Where(t => t.customerid == vDetail.CustomerID & t.device == vDetail.Address & t.valuekey == "MOTION" & t.C__createdAt > dStartTime).ToList();
            double[] xT = oTVals.Select(t => t.C__createdAt.DateTime.ToOADate()).ToArray();
            double[] yT = oTVals.Select(t => double.Parse(t.value, System.Globalization.CultureInfo.InvariantCulture)).ToArray();
            List<ChannelValues> oHVals = oDB.ChannelValues.Where(t => t.customerid == vDetail.CustomerID & t.device == vDetail.Address & t.valuekey == "BRIGHTNESS" & t.C__createdAt > dStartTime).ToList();
            DateTime[] xH = oHVals.Select(t => t.C__createdAt.DateTime).ToArray();
            int[] yH = oHVals.Select(t => int.Parse(t.value)).ToArray();
            //double[] yValues = { 10, 27.5, 7, 12, 45.5 };
            //string[] xNames = { "Mike", "John", "William", "George", "Alex" };
            Chart1.Series[0].Points.DataBindXY(xT, yT);
            Chart1.Series[1].Points.DataBindXY(xH, yH);
            Panel1.Visible = true;
            modpop1.Show();
        }

        protected void btClose_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }
    }
}